package com.qf.web.controller;

import com.qf.domain.Stu;
import com.qf.service.StuService;
import com.qf.service.impl.StuServiceImp;
import com.qf.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * projectName:test_stu
 * author:lcy
 * time:2023/12/8 15:53
 * description:
 */
@WebServlet("/addstu")
public class addStuServlet extends BaseServlet {
    public String addGoods(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("进入了该方法");
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String stuname = request.getParameter("stuname");
        String stuno = request.getParameter("stuno");
        String pwd = request.getParameter("pwd");
        String idcard = request.getParameter("idcard");
        String startdate = request.getParameter("startdate");
        String status = request.getParameter("status");
        System.out.println("stuname="+stuname);
        System.out.println("pwd="+pwd);


        if(StringUtils.isEmpty(stuname)){
            request.setAttribute("msg", "姓名不能为空");
            return "message.jsp";
        }
        if(StringUtils.isEmpty(stuno)){
            request.setAttribute("msg", "学号不能为空");
            return "message.jsp";
        }
        if(StringUtils.isEmpty(pwd)){
            request.setAttribute("msg", "密码不能为空");
            return "message.jsp";
        }
        if(StringUtils.isEmpty(idcard)){
            request.setAttribute("msg", "身份证号不能为空");
            return "message.jsp";
        }
        if(StringUtils.isEmpty(startdate)){
            request.setAttribute("msg", "入学日期不能为空");
            return "message.jsp";
        }
        if(StringUtils.isEmpty(status)){
            request.setAttribute("msg", "权限不能为空");
            return "message.jsp";
        }
        StuService stuService=new StuServiceImp();
        try {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            System.out.println(sdf.parse(startdate));
            Stu stu=new Stu(0,stuname,stuno,pwd,idcard,sdf.parse(startdate),Integer.parseInt(status));
            stuService.addStu(stu);
            return "selectstuservlet";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "失败" + e.getMessage());
            return "message.jsp";
        }
    }
}

